package demo_thread;

/**
 * @author: android 超级兵
 * @create: 2022-01-15 13:55
 * TODO Thread.isInterrupted
 **/
public class InterruptRunnableClient {

    public static int count = 0;

    public static class InterruptRunnableClientRunnable implements Runnable {
        @Override
        public void run() {
            // isInterrupted() 接受中断信息
            // isInterrupted = false 未中断
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    count++;
                    Thread.sleep(50);
                    System.out.println("count:" + count);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    System.out.println("报错了:" + e.getMessage()
                            + "\tisInterrupted:" + Thread.currentThread().isInterrupted());
                }
            }
            System.out.println("执行结束了");

        }
    }

    public static void main(String[] args) throws InterruptedException {
        InterruptRunnableClientRunnable runnable = new InterruptRunnableClientRunnable();

        Thread thread = new Thread(runnable);
        thread.start();

        System.out.println("等待3秒,请求中断");
        Thread.sleep(3000);

        // 发送中断请求
        thread.interrupt();
    }
}
